<!--  -->
<template>
    <el-dialog @close="close" title="提交模板反馈" :visible.sync="dialogVisible" :close-on-click-modal="false" width="600px" :append-to-body="true" class="dialog-form">
      <el-form :model="suggestion" v-loading="loading" :rules="rules" ref="suggestionForm" @submit.native.prevent>
        <el-form-item label="您的称呼" :label-width="formLabelWidth">
          <el-input size="medium" v-model="suggestion.name" maxlength="100" style="width:90%;" autocomplete="off"
            placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="联系方式" :label-width="formLabelWidth">
          <el-input size="medium" v-model="suggestion.contactInformation" maxlength="100" style="width:90%;" autocomplete="off"
            placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="您的行业" :label-width="formLabelWidth" prop="industry">
          <el-input size="medium" v-model="suggestion.industry" maxlength="100" style="width:90%;" autocomplete="off"
            placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="参考网址" :label-width="formLabelWidth" prop="referenceLink">
          <el-input size="medium" v-model="suggestion.referenceLink" maxlength="500" style="width:90%;" autocomplete="off"
            placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="反馈内容" :label-width="formLabelWidth">
          <el-input size="medium" v-model="suggestion.content" maxlength="500" style="width:90%;" type="textarea" :rows="3"
            placeholder=""></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="close">取 消</el-button>
        <el-button size="mini" type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
</template>

<script>
export default {
  data () {
    // 自定义校验
    var checkIndustry = (rule, value, callback) => {
      if (value.trim() === '') {
        callback(new Error('您的行业不能为空或都为空格'))
      } else {
        callback()
      }
    }
    var checkUrl = (rule, value, callback) => {
      if (value.trim() === '') {
        callback(new Error('请输入参考网址'))
      }
      if (!/^(http|https|\/\/)/.test(value)) {
        callback(new Error('需要http、https或者//开头'))
      }
      callback()
    }
    return {
      suggestion: {
        name: '',
        contactInformation: '',
        industry: '',
        referenceLink: '',
        content: ''
      },
      rules: {
        industry: [
          { required: true, message: '请输入您的行业', trigger: 'blur' },
          { validator: checkIndustry, trigger: 'blur' }
        ],
        referenceLink: [{ required: true, validator: checkUrl, trigger: 'blur' }]
      },
      formLabelWidth: '120px',
      loading: false,
      dialogVisible: false
    }
  },
  methods: {
    close () {
      if (this.$refs['suggestionForm']) {
        this.$refs['suggestionForm'].clearValidate()
      }
      this.suggestion = {
        name: '',
        contactInformation: '',
        industry: '',
        referenceLink: '',
        content: ''
      }
      this.loading = false
      this.dialogVisible = false
    },
    show () {
      this.dialogVisible = true
    },
    save () {
      this.$refs['suggestionForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          let suggestion = JSON.parse(JSON.stringify(this.suggestion))
          suggestion.url = JSON.stringify(this.suggestion.url)
          // 请求写入数据库
          this.$post('/template/append-suggestion', suggestion).then(response => {
            this.loading = false
            if (response) {
              this.dialogVisible = false
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">
.dialog-form /deep/ .el-dialog__body{
  padding-bottom: 0;
}
</style>
